package Inet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient_Dic {
    private Socket socket = null;

    public TcpEchoClient_Dic(String serverIp,int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }

    public void start() throws IOException {
        System.out.println("客户端上线！");

        try (InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream()){
            Scanner scanner = new Scanner(System.in);
            while (true) {
                String request = scanner.next();
                request += "\n";
                outputStream.write(request.getBytes());

                Scanner clientScanner = new Scanner(inputStream);
                if (!clientScanner.hasNext()) {
                    break;
                }
                String response = clientScanner.next();
                System.out.println(response);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient_Dic dic = new TcpEchoClient_Dic("127.0.0.1",9090);
        dic.start();
    }
}
